//
// Created by shiminli on 2024/1/8.
//

#ifndef DDOFFICE_DD_ENCRYPTION_H
#define DDOFFICE_DD_ENCRYPTION_H

#include <iomanip>
#include <sstream>
#include <CommonCrypto/CommonDigest.h>

class dd_encryption {
public:

    static std::string calculateMD5(const std::string &input) {
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5(input.c_str(), (CC_LONG) input.length(), digest);

//        char mdString[CC_MD5_DIGEST_LENGTH * 2 + 1];
//    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
//        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
//    return std::string(mdString);

        std::stringstream ss;
        for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
            ss << std::hex << std::setw(2) << std::setfill('0') << (int) digest[i];
        return ss.str();
    }

};

#endif //DDOFFICE_DD_ENCRYPTION_H
